param(
    [Parameter(Mandatory=$true)]
    [string]$serviceName,

    [Parameter(Mandatory=$true)]
    [string]$executablePath
)

# 检查执行文件是否存在
if (-not (Test-Path $executablePath)) {
    Write-Error "The specified executable path '$executablePath' does not exist. Please check if the path is correct."
    exit
}


# # 检查服务是否已存在，如果存在则先移除
if (Get-Service -Name $serviceName -ErrorAction SilentlyContinue) {
    Write-Host "Discover the existing service '$serviceName', remove it..."
    Stop-Service -Name $serviceName
    Remove-Service -Name $serviceName
    Write-Host "Service '$serviceName' has been removed."
}

# 安装 Windows 服务
New-Service -Name $serviceName -BinaryPathName $executablePath -StartupType Automatic

# 启动 Windows 服务
Start-Service -Name $serviceName

Write-Host "Windows service '$serviceName' has been successfully installed and started."